<template>
	<view>
		<view class="code-login">
			<view class="title">
				验证码登录
			</view>
			<u--form labelPosition="top" :model="model" :rules="rules" ref="uForm" :labelStyle="labelStyle">
				<u-form-item label="账号" prop="account">
					<u--input :customStyle="accountCustomStyle" v-model="model.account" border="none"
						placeholder="请输入手机号" />
				</u-form-item>
				<u-form-item label="验证码" prop="code">
					<!-- 注意：由于兼容性差异，如果需要使用前后插槽，nvue下需使用u--input，非nvue下需使用u-input -->
					<!-- #ifndef APP-NVUE -->
					<u-input :customStyle="accountCustomStyle" v-model="model.code" border="none" placeholder="请输入验证码">
					<!-- #endif -->
						<!-- #ifdef APP-NVUE -->
						<u--input :customStyle="accountCustomStyle" v-model="model.code" border="none" placeholder="请输入验证码">
						<!-- #endif -->
							<template slot="suffix">
								<u-code :seconds="seconds" @end="codeEnd" @start="codeStart" ref="uCode"
									@change="codeChange"></u-code>
								<text class="send-code" @click="handleClickSendCode">{{code}}</text>
							</template>
					<!-- #ifndef APP-NVUE -->
					</u-input>
					<!-- #endif -->
					<!-- #ifdef APP-NVUE -->
					</u--input>
					<!-- #endif -->
				</u-form-item>
				<!-- <view class="forgot-password" @click="handleClickForgotPassword">忘记密码</view> -->
			</u--form>
			<view class="to-register">
				还没有账号？去<text @click="handleClickRegister">注册</text>
			</view>
			<view class="account-button" @click="handleClickAccountLogin">登录</view>
		</view>
		<loginAgreement :chooes="agreement_chooes" @choose="(e)=>agreement_chooes=e"></loginAgreement>
	</view>
</template>

<script>
	import loginAgreement from './components/loginAgreement.vue';
	export default {
		components: {
			loginAgreement
		},
		data() {
			return {
				labelStyle: {
					fontSize: "32rpx",
					fontWeight: 500,
					marginLeft: "32rpx",
					width: "200rpx"
				},
				accountCustomStyle: {
					height: "96rpx",
					borderRadius: "24rpx",
					background: "rgba(246, 248, 252, 1)",
					padding: "0 36rpx",
				},
				model: {
					account: "",
					password: ""
				},
				passwordType: "password",
				rules: {
					account: [{
							required: true,
							message: '请输入账号',
							trigger: ['change', 'blur'],
						},
						// {
						// 	// 自定义验证函数，见上说明
						// 	validator: (rule, value, callback) => {
						// 		// 上面有说，返回true表示校验通过，返回false表示不通过
						// 		// uni.$u.test.mobile()就是返回true或者false的
						// 		return uni.$u.test.mobile(value);
						// 	},
						// 	message: '账号不正确',
						// 	// 触发器可以同时用blur和change
						// 	trigger: ['change', 'blur'],
						// }
					],
					code: [{
						required: true,
						message: "请输入验证码",
						trigger: ['change', 'blur']
					}]
				},
				//已阅读并同意
				agreement_chooes: true,
				code: "发送验证码",
				seconds: 60
			}
		},
		onReady() {
			//onReady 为uni-app支持的生命周期之一
			this.$refs.uForm.setRules(this.rules)
		},
		methods: {
			/**
			 * 账号登录
			 */
			handleClickAccountLogin() {
				this.$refs.uForm.validate().then(res => {
					uni.$u.toast('校验通过')
				}).catch(errors => {
					uni.$u.toast('校验失败')
				})
				console.log("账号登录");
			},
			// handleClickForgotPassword() {
			// 	console.log("忘记密码");
			// },
			handleClickRegister() {
				console.log("注册");
				this.goto('pages/public/register')
			},
			/**
			 * 发送验证码
			 */
			handleClickSendCode() {
				if (this.$refs.uCode.canGetCode) {
					// 模拟向后端请求验证码
					setTimeout(() => {
						uni.hideLoading();
						// 这里此提示会被this.start()方法中的提示覆盖
						uni.$u.toast('验证码已发送');
						// 通知验证码组件内部开始倒计时
						this.$refs.uCode.start();
					}, 2000);
				} else {
					uni.$u.toast('倒计时结束后再发送');
				}
			},
			codeEnd() {

			},
			codeStart() {

			},
			codeChange(text) {
				this.code = text;
			},
		}
	}
</script>

<style lang="scss" scoped>
	/* #ifdef H5 */
	page {
		position: relative;
		height: 100%;
		background-color: $page-color-base2;
	}
	/* #endif */

	.code-login {
		position: relative;
		width: 100%;
		background-color: $page-color-base2;
		
		.title {
			padding-top: 36rpx;
			margin: 0 36rpx 36rpx 72rpx;
			font-size: 60rpx;
			font-weight: 500;
		}

		::v-deep .u-form {
			margin: 40rpx;
		}

		.account {
			border: 1rpx red solid;
		}

		.forgot-password {
			margin-right: 50rpx;
			text-align: right;
			font-size: 28rpx;
			font-weight: 400;
		}

		.to-register {
			text-align: center;
			font-size: 28rpx;
			font-weight: 400;

			text {
				color: rgba(76, 212, 230, 1);
			}
		}

		.account-button {
			display: flex;
			justify-content: center;
			align-items: center;
			width: 335px;
			height: 44px;
			background: $theme-color;
			margin: 40rpx auto;
			text-align: center;
			border-radius: 24rpx;
			color: #FFF;
			font-size: 32rpx;
		}

		.send-code {
			font-size: 28rpx;
			font-weight: 400;
			color: rgba(76, 212, 230, 1);
		}
	}
</style>